<template>
  <PageWrapper contentFullHeight>
    <ListTemplate
      :register-table="registerTable"
      addauth="system:auxiliary:category:add"
      :method="methods"
    />
    <Handle @register="registerLayer" @success="tableMethods.reload" />
  </PageWrapper>
</template>
<script setup lang="ts">
  import ListTemplate from '@/views/common/operate/listTemplate.vue';

  import ApiV1 from '@/views/common/operate/ApiV1';
  import { createAsyncComponent } from '@/utils/factory/createAsyncComponent';

  import PageWrapper from '@/components/Page/src/PageWrapper.vue';
  import {
    apiGetCategoryDeletePath,
    apiPostCategoryListQuery,
  } from '@/api/http/system/auxiliary/category/category.api';
  import { useCategoryListColumns } from '@/api/http/system/auxiliary/category/category.data';
  import { listToTree } from '@/utils/helper/treeHelper';

  defineOptions({
    name: 'SystemAuxiliaryCategoryList',
  });
  const Handle = createAsyncComponent(() => import('./handle.vue'));
  const { registerTable, registerLayer, methods, tableMethods } = ApiV1.useList({
    tableProps: {
      api: apiPostCategoryListQuery,
      columns: useCategoryListColumns(),
      useSearchForm: false,
      rowKey: 'id',
      afterFetch: (rows) => {
        return listToTree(rows, { id: 'id', pid: 'pid', children: 'children' });
      },
    },
    editActionItem: (record) => {
      return {
        auth: 'system:auxiliary:category:edit',
        ifShow: () => record.pid !== -1,
      };
    },
    delActionItem: (record) => {
      return {
        auth: 'system:auxiliary:category:delete',
        ifShow: () => record.pid !== -1,
      };
    },
    mode: 'model',
    delFN: apiGetCategoryDeletePath,
    auth: 'system:auxiliary:category:list',
  });
</script>
